/* 
 * File:   XpackHandler.cpp
 * Author: minaev
 * 
 * Created on 9 Ноябрь 2013 г., 14:05
 */

#include "XpackHandler.h"
#include "Xservice.h"

#include "stdio.h"

XpackHandler::XpackHandler(XbeeContainer* xcont, XUDPShareMem* udpShmem) {
    this->xcont = xcont;
    this->udpShmem = udpShmem;
}

XpackHandler::~XpackHandler() {
}

void XpackHandler::addDetector(const pXpackDetector detector){
    detectors.push_back(detector);
}

void XpackHandler::procPack(Xpack* pack) {
//    static int nblock;
//    static uint8_t reqStat;
//    static uint8_t curStat;
//    static uint32_t buf;
    //Xservice::printArrayToHexStr((char*) (pack->getData() + 16), 2);
    Xservice::printArrayToHexStr(pack->getData(), pack->getLength());

    for(int i =0;i<detectors.size();i++){
        if(pack->getByte(0)==detectors[i]->getXpackType()){
            if(detectors[i]->isXpackValid(pack)){
                detectors[i]->action(pack);
            }
        }
    }
    //!!!replace to CONSTANTS: pack->getData() + X
//    
//    //ADC (0x92)
//    if (pack->getByte(0) == XAPI_RX_IO_RECV) {
//        nblock = xcont->findById(*(uint32_t*) (pack->getData() + 5));
//
//        if (nblock != -1) {
//            reqStat = *(uint8_t*) xcont->getParam(nblock, SLN_XREQSTAT);
//            curStat = *(uint8_t*) xcont->getParam(nblock, SLN_XSTAT);
//            
//            xcont->setParam(nblock, SLN_XADC, (void*) (pack->getData() + 18));
//            
//            //update button
//            /*buf = ((*(uint8_t*)(pack->getData() + 17) & 0x2)>>1);
//            if(buf == 0){
//                xcont->setParam(nblock, SLN_XBTNS, (void*) &(buf=1) );
//            }
//            
//            if((*(uint8_t*)xcont->getParam(nblock,SLN_XBTNS)) == 1){
//                Xmsg::printf("ALARM button!!!\n");
//                xcont->sendBtnStatusMessage(nblock);
//            }*/
//            
//            if(*(uint8_t*)xcont->getParam(nblock,SLN_XBTNS) !=
//                    *(uint8_t*)xcont->getParam(nblock,SLN_XBTNOLDS)){
//                if((*(uint8_t*)xcont->getParam(nblock,SLN_XBTNS)) == 1){
//                    xcont->pinAlarmOn(nblock);
//                }else{
//                    xcont->pinAlarmOff(nblock);
//                }
//                xcont->setParam(nblock,SLN_XBTNOLDS,
//                        xcont->getParam(nblock,SLN_XBTNS));
//            }
//            
//            if (*(uint16_t*) xcont->getParam(nblock, SLN_XFIXADC) == 0) {
//                Xmsg::printf("init fix adc\n");
//                xcont->setParam(nblock, SLN_XFIXADC, (void*) (pack->getData() + 18));
//            }
//            
//            if((curStat == STAT_IDLE || curStat == STAT_POWEROFF) && reqStat==curStat){
//                xcont->stopScan(nblock);
//            }
//            
//            if (xcont->isAlarm(nblock) && curStat == STAT_SCAN) {
//                Xmsg::printf("OLIARM!!!\n");
//                xcont->setParam(nblock, SLN_XREQSTAT, &(buf = STAT_ALARM));
//                xcont->toNewStatus(nblock);
//            }
//            if (curStat == STAT_ALARM) {
//                xcont->sendAlarmMessage(nblock);
//            }
//        } /*else {
//            //registration Xbee
//            if (*(uint32_t*) (pack->getData() + 5) != 0x0) {
//                Xmsg::printf("registration: 0x%x\n", *(uint32_t*) (pack->getData() + 5));
//                nblock = xcont->addXbee(*(uint32_t*) (pack->getData() + 5));
//                xcont->setParam(nblock, SLN_XADC, (void*) (pack->getData() + 16));
//            }
//        }*/
//        //shmem->printMem();
//    }
//
//    //PING (0x97)
//    if (pack->getByte(0) == XAPI_RC_RESP) {
//        if (pack->getByte(12) == 'S' && pack->getByte(13) == 'L') {
//            nblock = xcont->findById(*(uint32_t*) (pack->getData() + 6));
//            if (nblock == -1) {
//                if (*(uint32_t*) (pack->getData() + 6) != 0) {
//                    Xmsg::printf("registration: 0x%x\n", *(uint32_t*) (pack->getData() + 6));
//                    nblock = xcont->addXbee(*(uint32_t*) (pack->getData() + 6));
//                }
//            } else {
//                buf = *(uint8_t*) xcont->getParam(nblock, SLN_XPINGS) + 1;
//                xcont->setParam(nblock, SLN_XPINGS, &buf);
//            }
//        }
//    }
}
